Page({
	onHide(){
		this.save()
	},
	onUnload(){
		this.save()
	},
	save(){
		if(!this.hasSave){
			this.hasSave=true
			let data={...this.data}
			delete data.__webviewId__
			wx.setStorageSync(this.key, data)
		}
	},
	async onLoad(e){
		this.host='https://www.hbqcwk.com'
		this.key='dahai'
		let data=wx.getStorageSync(this.key) //读取缓存的数据
		if(data)this.setData(data) //若缓存有数据，就还原原来的数据
	},
	menus(e){ //菜单项的点击操作
		let index=e.target.dataset.index //获取点击菜单的索引号
		if(index==null)return //没有点击具体的菜单项
		this.hasSave=false
		let oldIndex=this.data.menuIndex //原来点击的索引号
		let change={menuIndex:index} //用来记录有变化的数据
		change[`menus[${oldIndex}].selected`]=false
		change[`menus[${index}].selected`]=true
		this.setData(change)
	},
	async types(e){//类型项的点击操作
		let index=e.target.dataset.index //获取点击类型的索引号
		if(index==null)return //没有点击具体的菜单项
		let menuIndex=this.data.menuIndex //当前菜单项的索引号
		let menu=this.data.menus[menuIndex] //当前的菜单项
		let oldIndex=menu.typeIndex //原来点击类型的索引号
		let change={} //用来记录有变化的数据
		if(oldIndex!=null)change[`menus[${menuIndex}].types[${oldIndex}].selected`]=false
		change[`menus[${menuIndex}].types[${index}].selected`]=true
		change[`menus[${menuIndex}].typeIndex`]=index
		this.setData(change)
		let type=menu.types[index] //获取点击的类型对象
		if(type.list && type.list.length>0) return //如果该类型下有视频列表数据，就直接返回
		this.hasSave=false
		// https://www.hbqcwk.com/class/13.html
		let url=`${this.host}/class/${type.id}.html` 
		let app=getApp()
		let html=await app.request({url})
		let r, list=[], regex=/href="([^"]+)" title="([^"]+)" data-original="([^"]+)/g  //g-全局匹配
		while(r=regex.exec(html)){
			list.push({
				url: r[1],
				title: r[2],
				img: r[3],
			})
		}
		// change={}
		// change[`menus[${menuIndex}].types[${index}].list`]=list
		// this.setData(change)
		console.log(list)
		this.setData({[`menus[${menuIndex}].types[${index}].list`]:list})
	},
	data: { //页面的初始数据
		menuIndex:0,
		menus:[{
			name:'电影',
			selected: true,
			types:[
				{ name:'动作片', id:6, list:[] },
				{ name:'喜剧片', id:7 },
				{ name:'爱情片', id:8 },
				{ name:'科幻片', id:9 },
				{ name:'恐怖片', id:10 },
				{ name:'剧情片', id:11 },
				{ name:'战争片', id:12 },
				{ name:'动画片', id:24 },
				{ name:'纪录片', id:23 },
			]
		},{
			name:'电视剧',
			types:[
				{ name:'国产剧', id:13 },
				{ name:'香港剧', id:14 },
				{ name:'韩国剧', id:15 },
				{ name:'欧美剧', id:16 },
				{ name:'日本剧', id:21 },
				{ name:'其它剧', id:22 },
			]
		},{
			name:'综艺',
			types:[
				{ name:'大陆综艺', id:25 },
				{ name:'日韩综艺', id:26 },
				{ name:'港台综艺', id:27 },
				{ name:'欧美综艺', id:28 },
			]
		},{
			name:'动漫',
			types:[
				{ name:'国产动漫', id:29 },
				{ name:'日韩动漫', id:30 },
				{ name:'欧美动漫', id:31 },
				{ name:'其它动漫', id:32 },
			]
		},{
			name:'短剧',
			types:[
				{ name:'女频恋爱', id:34 },
				{ name:'反转爽剧', id:35 },
				{ name:'古装仙侠', id:36 },
				{ name:'年代穿越', id:37 },
				{ name:'脑洞悬疑', id:38 },
				{ name:'现代都市', id:39 },
			]
		},]
	},

})